package com.mastercard.assets.web.struts.actions;

import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
import com.mastercard.assets.jpa.TblAsset;
import com.mastercard.assets.jpa.TblCampania;
import com.mastercard.assets.jpa.TblCampaniaMes;
import com.mastercard.assets.jpa.TblCampaniaProductoAdq;
import com.mastercard.assets.jpa.TblCampaniaProductoUsuario;
import com.mastercard.assets.jpa.TblPaquete;
import com.mastercard.assets.jpa.TblProducto;
import com.mastercard.assets.util.MCUtil;
import com.mastercard.assets.util.dao.MasterCardDAO;
import com.mastercard.assets.web.struts.forms.ContratoForm;
import com.mastercard.assets.web.util.to.TblAssetWraper;
import com.mastercard.assets.web.util.to.UserLogin;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


public class Contrato 
        extends
		Action
{
 
        public ActionForward execute(
							ActionMapping mapping , 
							ActionForm form ,
							HttpServletRequest request , 
							HttpServletResponse response )
		throws 
			IOException , 
			ServletException
        {
            
            HttpSession session = request.getSession( ) ;
		
		// en caos de null
		if( session == null )
			return mapping.findForward( "noLogin" ) ;
		
		// toma el userLogin
		UserLogin user = ( UserLogin ) session.getAttribute( "UserLogin" ) ;
		
		// en caso de null
		if( user == null )
			return mapping.findForward( "noLogin" ) ;
                
                // formulario
                ContratoForm f = ( ContratoForm ) form ;
                
                int cveCampania = f.getCveCamapnia( ) ;
                String employees = " " , Cardholders = " " ;
                
                if( f.getEmployees( ) != null ) { employees = "X" ; }
                if( f.getCardholders( ) != null ) { Cardholders = "X" ; }
                
                TblCampania campania = MasterCardDAO.findCampaignById( cveCampania ) ;
                
                campania.setNombre( MCUtil.TextoFormateado( campania.getNombre( ) ) ) ;
                campania.setTexto(MCUtil.TextoFormateado( campania.getTexto( ) ) ) ;
                
                // toma todos los registros correspondientes de tblCampaniaProductoUsuario
                List< TblCampaniaProductoUsuario > tblCampaniaProductoUsuario = MasterCardDAO.getAllActiveCampaignProdUsrByCampaingId( campania.getCveCampania( ) ) ; 
                
                // toma todos los registros correspondientes de tblCampaniaProductoAdq
                List< TblCampaniaProductoAdq > tblCampaniaProductoAdq = MasterCardDAO.getAllActiveCampaignProdAdqByCampaingId( campania.getCveCampania( ) ) ; 
                
                MasterCardDAO.closeEntityManager( ) ;
                
                int tamanio = campania.getMeses( ) ;
                int[] meses = new int[tamanio] ;
                int[] ttp = new int[campania.getTblProductos( ).size( )] ;
                int totalp = 0 ;
                int tMes = 0 ;
                int vtar = 0 ;
                
                List<TblCampaniaMes> TblCampaniaMes = new ArrayList< TblCampaniaMes >( ) ;
                
                //List< TblCampaniaMes > tcm = new ArrayList< TblCampaniaMes >( ) ;
                if ( tblCampaniaProductoUsuario != null )
                {
                        for ( TblCampaniaProductoUsuario tcpu : tblCampaniaProductoUsuario )
                    {
                        for ( TblCampaniaMes tcm : tcpu.getTblCampaniaMeses( ) )
                        {
                            TblCampaniaMes.add( tcm ) ;
                        }
                    }
                }
                
                for (int x = 0 ; x < tamanio  ; x++  )
                {
                    meses[ x ] = x + 1 ;
                }
                
                
                    for ( TblProducto tp : campania.getTblProductos() )
                    {
                        
                        for (TblCampaniaProductoAdq tca :  tblCampaniaProductoAdq )
                        {
                            vtar = tca.getTarjetas( ) ;
                            totalp += vtar ;
                            if ( tca.getTblProducto( ).getCveProducto( ) == tp.getCveProducto( ) )
                            {
                                ttp[ tMes ] += tca.getTarjetas( ) ; 
                            }
                        }
                        tMes += 1 ;
                    }                    
                
                
                int total = totalp / 2 ;
                
                // toma todos los paquetes
                List< TblPaquete > paquetes = campania.getTblPaquetes( ) ;
                
                List< TblAssetWraper > assetS = new ArrayList< TblAssetWraper >( ) ;
                
                // recorre los paquetes
		for( TblPaquete paq : paquetes )
		{
			// toma el asset al que pertenece el paquete
			TblAsset asset = paq.getTblAsset( ) ;
			
			// en caso del null
			if( asset == null )
				// siguiente
				continue ;
			
			// hace la comparacion con los que ya estan
			boolean yaEsta = false ;
			
			TblAssetWraper wraper = null ;
			
			// recorre los assets
			for( TblAssetWraper as : assetS )
				// en el caso de que ya haya una igual
				if( as.getCveAsset( ) == asset.getCveAsset( ) )
				{
					// cambia bandera
					yaEsta = true ;
					
					wraper = as ;
					
					// quiebra el ciclo
					break ;
				}
			
			// en caso de que no exista
			if( ! yaEsta )
				// lo agrega
				assetS.add( new TblAssetWraper( asset ) ) ;
			// en caso de que si exista
			else
				wraper.addPackage( ) ;
				
		}
                
                String relativeWebPath = "/images/Dibujo.png";
                String absoluteDiskPath = getServlet( ).getServletContext().getRealPath( relativeWebPath ) ;
                System.out.println("Ruta::: " + absoluteDiskPath ) ;
                
                Document documento = new Document(PageSize.A4, 72, 72, 86, 86) ;
                
                try
                {   
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    PdfWriter writer = PdfWriter.getInstance( documento, baos ) ;                    
                    
                    documento.open();
                    
                    Font font1 = new Font(Font.FontFamily.TIMES_ROMAN  , 15, Font.BOLD);
                    Font font2 = new Font(Font.FontFamily.TIMES_ROMAN  , 7, Font.BOLD);
                    Font font3 = new Font(Font.FontFamily.TIMES_ROMAN  , 7, Font.NORMAL);
                    
                    Paragraph salto = new Paragraph( "  " , font1 ) ;
                    
                    Paragraph parrafo = new Paragraph("Marketing Initiative Confirmation", font1) ;
                    parrafo.setAlignment(Paragraph.ALIGN_CENTER);
                                        
                    documento.add( parrafo ) ;
                    documento.add( salto ) ;
                    
                    
                    PdfPTable tabla2 = new PdfPTable(2);
                    tabla2.setWidthPercentage(100);
                    PdfPCell cell1_T2 = new PdfPCell( new Paragraph( "Name: " , font2 ) ) ;
                    PdfPCell cell2_T2 = new PdfPCell( new Paragraph( "" + campania.getTblUsuario2( ).getNombre( ) , font3 ) ) ;
                    PdfPCell cell3_T2 = new PdfPCell( new Paragraph( "Department: " , font2 ) ) ;
                    PdfPCell cell4_T2 = new PdfPCell( new Paragraph( " "  , font3 ) ) ;
                    tabla2.addCell( cell1_T2 ) ; tabla2.addCell( cell2_T2 ) ; tabla2.addCell( cell3_T2 ) ; tabla2.addCell( cell4_T2 ) ;
                    
                    PdfPTable tabla3 = new PdfPTable(4); 
                    tabla3.setWidthPercentage(100);
                    PdfPCell cell1_T3 = new PdfPCell( new Paragraph( "Event Date: " , font2 ) ) ;
                    PdfPCell cell2_T3 = new PdfPCell( new Paragraph( " " , font3 ) ) ;
                    PdfPCell cell3_T3 = new PdfPCell( new Paragraph( "Results Due Date: " , font2 ) ) ;
                    PdfPCell cell4_T3 = new PdfPCell( new Paragraph( "" + campania.getFechaFin( ).toString( ).substring(0, 10) , font3 ) ) ;
                    PdfPCell cell5_T3 = new PdfPCell( new Paragraph( "Target Start Date: " , font2 ) ) ;
                    PdfPCell cell6_T3 = new PdfPCell( new Paragraph( "" + campania.getFechaIni( ).toString( ).substring(0, 10) , font3 ) ) ;
                    PdfPCell cell7_T3 = new PdfPCell( new Paragraph( "Names Due Date: " , font2 ) ) ;
                    PdfPCell cell8_T3 = new PdfPCell( new Paragraph( " " , font3 ) ) ;
                    PdfPCell cell9_T3 = new PdfPCell( new Paragraph( "Target End Date: " , font2 ) ) ;
                    PdfPCell cell10_T3 = new PdfPCell( new Paragraph( "" + campania.getFechaFin( ).toString( ).substring(0, 10) , font3 ) ) ;
                    PdfPCell cell11_T3 = new PdfPCell( new Paragraph( "Channel: " , font2 ) ) ;
                    PdfPCell cell12_T3 = new PdfPCell( new Paragraph( "" + f.getChannel( ) , font3 ) ) ;
                    PdfPCell cell13_T3 = new PdfPCell( new Paragraph( "" ) ) ;
                    PdfPCell cell14_T3 = new PdfPCell( new Paragraph( "" ) ) ;
                    PdfPCell cell15_T3 = new PdfPCell( new Paragraph( "Target Public: " , font2 ) ) ;
                    PdfPCell cell16_T3 = new PdfPCell( new Paragraph( "" , font3 ) ) ;
                    cell13_T3.setBorder( 0 ) ; cell14_T3.setBorder( 0 ) ;
                    tabla3.addCell( cell1_T3 ) ; tabla3.addCell( cell2_T3 ) ; tabla3.addCell( cell3_T3 ) ; tabla3.addCell( cell4_T3 ) ;
                    tabla3.addCell( cell5_T3 ) ; tabla3.addCell( cell6_T3 ) ; tabla3.addCell( cell7_T3 ) ; tabla3.addCell( cell8_T3 ) ;
                    tabla3.addCell( cell9_T3 ) ; tabla3.addCell( cell10_T3 ) ; tabla3.addCell( cell11_T3 ) ; tabla3.addCell( cell12_T3 ) ;
                    tabla3.addCell( cell13_T3 ) ; tabla3.addCell( cell14_T3 ) ; tabla3.addCell( cell15_T3 ) ; tabla3.addCell( cell16_T3 ) ;
                    
                    PdfPTable tabla4 = new PdfPTable(4); 
                    tabla4.setWidthPercentage(100);
                    PdfPCell cell1_T4 = new PdfPCell( new Paragraph( "Employees: " , font2 ) ) ;
                    PdfPCell cell2_T4 = new PdfPCell( new Paragraph( "" + employees , font3 ) ) ;
                    PdfPCell cell3_T4 = new PdfPCell( new Paragraph( "Dept/Business Unit: " , font2 ) ) ;
                    PdfPCell cell4_T4 = new PdfPCell( new Paragraph( "" + f.getBcUnit( ) , font3 ) ) ;
                    PdfPCell cell5_T4 = new PdfPCell( new Paragraph( "Cardholders:  " , font2 ) ) ;
                    PdfPCell cell6_T4 = new PdfPCell( new Paragraph( "" + Cardholders , font3 ) ) ;
                    PdfPCell cell7_T4 = new PdfPCell( new Paragraph( "Consumer Segment/Product: " , font2 ) ) ;
                    PdfPCell cell8_T4 = new PdfPCell( new Paragraph( "" + f.getcSegment( ) , font3 ) ) ;
                    PdfPCell cell9_T4 = new PdfPCell( new Paragraph( "Other: " , font2 ) ) ;
                    PdfPCell cell10_T4 = new PdfPCell( new Paragraph( "" + f.getOther( ) , font3 ) ) ;
                    PdfPCell cell11_T4 = new PdfPCell( new Paragraph( "Define: " , font2 ) ) ;
                    PdfPCell cell12_T4 = new PdfPCell( new Paragraph( " " + f.getDefine( ) , font3 ) ) ;
                    tabla4.addCell( cell1_T4 ) ; tabla4.addCell( cell2_T4 ) ; tabla4.addCell( cell3_T4 ) ; tabla4.addCell( cell4_T4 ) ;
                    tabla4.addCell( cell5_T4 ) ; tabla4.addCell( cell6_T4 ) ; tabla4.addCell( cell7_T4 ) ; tabla4.addCell( cell8_T4 ) ;
                    tabla4.addCell( cell9_T4 ) ; tabla4.addCell( cell10_T4 ) ; tabla4.addCell( cell11_T4 ) ; tabla4.addCell( cell12_T4 ) ;
                    
                    PdfPTable tabla5 = new PdfPTable(4); 
                    tabla5.setWidthPercentage(100);
                    PdfPCell cell1_T5 = new PdfPCell( new Paragraph( "Yes" , font2 ) ) ;
                    PdfPCell cell2_T5 = new PdfPCell( new Paragraph( "" + f.getApprovalSI( ) , font3 ) ) ;
                    PdfPCell cell3_T5 = new PdfPCell( new Paragraph( "No" , font2 ) ) ;
                    PdfPCell cell4_T5 = new PdfPCell( new Paragraph( "" + f.getApprovalNO( ) , font3 ) ) ;
                    tabla5.addCell( cell1_T5 ) ; tabla5.addCell( cell2_T5 ) ; tabla5.addCell( cell3_T5 ) ; tabla5.addCell( cell4_T5 ) ;
                    
                    PdfPTable tabla6 = new PdfPTable(4);
                    tabla6.setWidthPercentage(100);
                    if( campania.getTblTipoCampania( ).getCveTipoCampania( ) == 2  )
                    {
                        int count = 1 ;
                        for( TblProducto prods : campania.getTblProductos( )  )
                        {
                            tabla6.addCell( new Paragraph( "Type of Program ("+ count +"): " , font2 ) );
                            tabla6.addCell( new Paragraph( "Business As Usual: " , font2 ) ) ;
                            tabla6.addCell( new Paragraph( "Estimated Incremental: " , font2 ) ) ;
                            tabla6.addCell( new Paragraph( "Total New Cards: " , font2 ) ) ;
                            
                            tabla6.addCell( new Paragraph( "" + prods.getNombre( ) , font2 ) ) ;
                            tabla6.addCell( new Paragraph( " " , font2 ) ) ;
                            for( int y = 0 ; y < ttp.length ; y++ )
                            {
                                if( ( count -1 ) == y )
                                {
                                    tabla6.addCell( new Paragraph( "" + ttp[ y ] , font2 ) ) ;
                                    tabla6.addCell( new Paragraph( "" + ttp[ y ] , font2 ) ) ;
                                }
                            }
                            
                            count += 1 ;
                        }
                        
                    }else if( campania.getTblTipoCampania( ).getCveTipoCampania( ) != 7 && 
                            campania.getTblTipoCampania( ).getCveTipoCampania( ) != 2 )
                    {
                        int count = 1 ;
                        for( TblProducto prods : campania.getTblProductos( )  )
                        {
                            tabla6.addCell( new Paragraph( "Type of Program ("+ count +"): " , font2 ) );
                            tabla6.addCell( new Paragraph( "Business As Usual: " , font2 ) ) ;
                            tabla6.addCell( new Paragraph( "Estimated Incremental: " , font2 ) ) ;
                            tabla6.addCell( new Paragraph( "Total New Cards: " , font2 ) ) ;
                            
                            tabla6.addCell( new Paragraph( "" + prods.getNombre( ) , font2 ) ) ;
                            tabla6.addCell( new Paragraph( " " , font2 ) ) ;
                            for( TblCampaniaProductoUsuario cpu : campania.getTblCampaniaProductoUsuarios( ) )
                            {
                                if( cpu.getTblProducto( ).getCveProducto( ) == prods.getCveProducto( ) )
                                {
                                    tabla6.addCell( new Paragraph( "US$" + cpu.getGdvTotalDelProducto() , font2 ) ) ;
                                    tabla6.addCell( new Paragraph( "US$" + cpu.getGdvTotalDelProducto() , font2 ) ) ;
                                }
                            }
                            
                            count += 1 ;
                        }
                        
                    }
                    
                PdfPTable tabla7 = new PdfPTable(2);
                tabla7.setWidthPercentage(100);
                tabla7.addCell( new Paragraph( "Asset " , font2 ) ) ;
                tabla7.addCell( new Paragraph( "Packages " , font2 ) ) ;                
                for( TblAssetWraper as : assetS )
                {
                    tabla7.addCell( new Paragraph( "" + as.getNombre( ) , font3 ) ) ;
                    tabla7.addCell( new Paragraph( "" + as.getQuantityOfPackage( ) , font3 ) ) ;
                }                
                tabla7.addCell( new Paragraph( "Budget Origin  " , font2 ) ) ;
                tabla7.addCell( new Paragraph( "" + f.getBudget( ) , font3 ) ) ;                
                tabla7.addCell( new Paragraph( "# of Assets (Tickets) " , font3 ) ) ;
                tabla7.addCell( new Paragraph( "" + campania.getTblPaquetes( ).size( )  , font3 ) ) ;
                    
                
                PdfPTable tabla8 = new PdfPTable(2);
                tabla8.setWidthPercentage(100);
                PdfPCell cell1_T8 = new PdfPCell( new Paragraph( "Estimated Implementation Costs US$ " , font2 ) ) ;
                PdfPCell cell2_T8 = new PdfPCell( new Paragraph( " " ) ) ;
                tabla8.addCell( cell1_T8 ) ; tabla8.addCell( cell2_T8 ) ;
                    
                    PdfPTable tabla = new PdfPTable(2);
                    tabla.setWidthPercentage(100);
                    PdfPCell cell1 = new PdfPCell( new Paragraph( "Customer Name/Strategic Partner: " , font2 ) ) ;
                    PdfPCell cell2 = new PdfPCell( new Paragraph( " " + campania.getTblCuenta( ).getDescripcion( ) , font3 ) ) ;
                    
                    PdfPCell cell3 = new PdfPCell( new Paragraph( "Customer Contact: " , font2 ) ) ;
                    cell3.setVerticalAlignment( Paragraph.ALIGN_MIDDLE ) ;
                    PdfPCell cell4 = new PdfPCell( new Paragraph( "" ) ) ;
                    cell4.addElement( tabla2 ) ;
                    
                    PdfPCell cell5 = new PdfPCell(new Paragraph("Name of Initiative: " , font2) );
                    PdfPCell cell6 = new PdfPCell(new Paragraph("" + campania.getNombre( ) , font3) );
                    
                    PdfPCell cell7 = new PdfPCell(new Paragraph
                            ("Clear and Full Description of the Mechanics (how the Initiative will work): ", font2) ) ;
                    PdfPCell cell8 = new PdfPCell(new Paragraph(" ") );
                    
                    PdfPCell cell9 = new PdfPCell(new Paragraph("Marketing Initiative Period: ", font2) ) ;
                    cell9.setVerticalAlignment(Paragraph.ALIGN_MIDDLE);
                    PdfPCell cell10 = new PdfPCell(new Paragraph("") );
                    cell10.addElement( tabla3 ) ;
                    
                    PdfPCell cell11 = new PdfPCell(new Paragraph("Activity Type: (please circle) " , font2) );
                    PdfPCell cell12 = new PdfPCell(new Paragraph(" " + campania.getTblTipoCampania( ).getNombre( ) , font3) );
                    
                     PdfPCell cell13 = new PdfPCell(new Paragraph("Eligibility: " , font2) );
                    cell13.setVerticalAlignment(Paragraph.ALIGN_MIDDLE);
                    PdfPCell cell14 = new PdfPCell(new Paragraph(" ") );
                    cell14.addElement( tabla4 ) ;
                    
                    PdfPCell cell15 = new PdfPCell(new Paragraph("Eligibility: " , font2) );
                    cell15.setVerticalAlignment(Paragraph.ALIGN_MIDDLE);
                    PdfPCell cell16 = new PdfPCell(new Paragraph(" ") );
                    cell16.addElement( tabla5 ) ;
                    
                    PdfPCell cell17 = new PdfPCell( ) ;
                    PdfPCell cell18 = new PdfPCell( ) ;
                    if( campania.getTblTipoCampania( ).getCveTipoCampania( ) == 2 )
                    {                         
                        cell17 = new PdfPCell(new Paragraph( "New Cards: " , font2) );
                        cell17.setVerticalAlignment(Paragraph.ALIGN_MIDDLE);
                        cell18 = new PdfPCell(new Paragraph(" ") );
                        cell18.addElement( tabla6 ) ;
                        
                    }else if( campania.getTblTipoCampania( ).getCveTipoCampania( ) == 7 )
                    { 
                        cell17 = new PdfPCell(new Paragraph( " " ) ) ;
                        cell18 = new PdfPCell(new Paragraph( " " ) ) ;
                        
                        
                    }else {                        
                        cell17 = new PdfPCell(new Paragraph( "GDV: " , font2) );
                        cell17.setVerticalAlignment(Paragraph.ALIGN_MIDDLE);
                        cell18 = new PdfPCell(new Paragraph(" ") );
                        cell18.addElement( tabla6 ) ;
                    }
                    
                    PdfPCell cell19 = new PdfPCell(new Paragraph("Target Assumptions Rationale: " , font2) );
                    PdfPCell cell20 = new PdfPCell(new Paragraph("" + f.getAssumptions( ) , font3) );
                    
                    PdfPCell cell21 = new PdfPCell(new Paragraph("MasterCard Support: " , font2));
                    cell21.setVerticalAlignment(Paragraph.ALIGN_MIDDLE);
                    PdfPCell cell22 = new PdfPCell(new Paragraph("") );
                    cell22.addElement( tabla7 ) ;
                    
                    PdfPCell cell23 = new PdfPCell(new Paragraph("Confirmation of Customer Contribution: " , font2));
                    cell21.setVerticalAlignment(Paragraph.ALIGN_MIDDLE);
                    PdfPCell cell24 = new PdfPCell(new Paragraph("") );
                    cell24.addElement( tabla8 ) ;
                    
                    PdfPCell cell25 = new PdfPCell(new Paragraph("Details of Investment (please comment and identify type of investment "
                        + "- communication costs: radio / tv / direct mail, travel expenses, any other additional  " , font2) );
                    PdfPCell cell26 = new PdfPCell(new Paragraph(" ") );

                    PdfPCell cell27 = new PdfPCell(new Paragraph("MasterCard: " , font2) );
                    PdfPCell cell28 = new PdfPCell(new Paragraph("Strategic Partner: " , font2) );

                    PdfPCell cell29 = new PdfPCell(new Paragraph("MasterCard International Incorporated " , font2) );
                    PdfPCell cell30 = new PdfPCell(new Paragraph("Insert Legal Entity Name " , font2) );

                    PdfPCell cell31 = new PdfPCell(new Paragraph("By: " , font2) );
                    PdfPCell cell32 = new PdfPCell(new Paragraph("By: " , font2) );

                    PdfPCell cell33 = new PdfPCell(new Paragraph("Name: " , font2) );
                    PdfPCell cell34 = new PdfPCell(new Paragraph("Name: " , font2) );

                    PdfPCell cell35 = new PdfPCell(new Paragraph("Title: " , font2) );
                    PdfPCell cell36 = new PdfPCell(new Paragraph("Title: " , font2) );

                    PdfPCell cell37 = new PdfPCell(new Paragraph("Date: " , font2) );
                    PdfPCell cell38 = new PdfPCell(new Paragraph("Date: " , font2) );

                    PdfPCell cell39 = new PdfPCell(new Paragraph( "" ) );
                    PdfPCell cell40 = new PdfPCell(new Paragraph("Strategic Partner: " , font2) );

                    PdfPCell cell41 = new PdfPCell(new Paragraph( "" ) );
                    PdfPCell cell42 = new PdfPCell(new Paragraph("Strategic Partner Address: " , font2) ) ;
                    
                    cell1.setBorder( 0 ) ; cell2.setBorder( 0 ) ; cell3.setBorder( 0 ) ; cell4.setBorder( 0 ) ; cell5.setBorder( 0 ) ;
                    cell6.setBorder( 0 ) ; cell7.setBorder( 0 ) ; cell8.setBorder( 0 ) ; cell9.setBorder( 0 ) ; cell10.setBorder( 0 ) ;
                    cell11.setBorder( 0 ) ; cell12.setBorder( 0 ) ; cell13.setBorder( 0 ) ; cell14.setBorder( 0 ) ; cell15.setBorder( 0 ) ;
                    cell16.setBorder( 0 ) ; cell17.setBorder( 0 ) ; cell18.setBorder( 0 ) ; cell19.setBorder( 0 ) ; cell20.setBorder( 0 ) ;
                    cell21.setBorder( 0 ) ; cell22.setBorder( 0 ) ; cell23.setBorder( 0 ) ; cell24.setBorder( 0 ) ; 
//                    cell25.setBorder( 0 ) ;
//                    cell26.setBorder( 0 ) ; cell27.setBorder( 0 ) ; cell28.setBorder( 0 ) ; cell29.setBorder( 0 ) ; cell30.setBorder( 0 ) ;
//                    cell31.setBorder( 0 ) ; cell32.setBorder( 0 ) ; cell33.setBorder( 0 ) ; cell34.setBorder( 0 ) ; cell35.setBorder( 0 ) ;
//                    cell36.setBorder( 0 ) ; cell37.setBorder( 0 ) ; cell38.setBorder( 0 ) ; cell39.setBorder( 0 ) ; cell40.setBorder( 0 ) ;
//                    cell41.setBorder( 0 ) ; cell42.setBorder( 0 ) ;

                    tabla.addCell(cell1); tabla.addCell(cell2); tabla.addCell(cell3); tabla.addCell(cell4); tabla.addCell(cell5);
                    tabla.addCell(cell6); tabla.addCell(cell7); tabla.addCell(cell8); tabla.addCell(cell9); tabla.addCell(cell10);
                    tabla.addCell(cell11); tabla.addCell(cell12); tabla.addCell(cell13); tabla.addCell(cell14); tabla.addCell(cell15);
                    tabla.addCell(cell16); tabla.addCell(cell17); tabla.addCell(cell18); tabla.addCell(cell19); tabla.addCell(cell20);
                    tabla.addCell(cell21); tabla.addCell(cell22); tabla.addCell(cell23); tabla.addCell(cell24); tabla.addCell(cell25);
                    tabla.addCell(cell26); tabla.addCell(cell27); tabla.addCell(cell28); tabla.addCell(cell29); tabla.addCell(cell30);
                    tabla.addCell(cell31); tabla.addCell(cell32); tabla.addCell(cell33); tabla.addCell(cell34); tabla.addCell(cell35);
                    tabla.addCell(cell36); tabla.addCell(cell37); tabla.addCell(cell38); tabla.addCell(cell39); tabla.addCell(cell40);
                    tabla.addCell(cell41); tabla.addCell(cell42);
                    
                    documento.add( tabla ) ;
                
                documento.newPage( ) ;
                
                
                
                Image clausulas = Image.getInstance( absoluteDiskPath ) ;
                
                documento.add( clausulas ) ;
//                --Paragraph titulo = new Paragraph("Schedule 1:", font1) ;
//                titulo.setAlignment(Paragraph.ALIGN_CENTER);
//                documento.add( titulo ) ;
//                
//                documento.add( new Paragraph("   ", font1) ) ;
//                
//                Paragraph subtitulo = new Paragraph(" Marketing Initiative Terms and Conditions  ", font2) ;
//                subtitulo.setAlignment(Paragraph.ALIGN_CENTER) ;
//                documento.add( subtitulo ) ;
//                
//                documento.add( new Paragraph("   ", font2) ) ;
//                
//                Paragraph c1 = new Paragraph("1. Use of Support: Strategic Partner"
//                        + " agrees that it will use Support provided by MasterCard under this Agreement only as set forth herein ", font3) ;
//                c1.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c1 ) ;
//                
//                Paragraph c2 = new Paragraph("and in no event will use such Support to "
//                        + "promote Competing Brands or Competing Card programs. For this purpose, 'Competing Brand' means  ", font3) ;
//                c2.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c2 ) ;
//                
//                Paragraph c3 = new Paragraph("Visa, American Express, Diners Club, JCB, Carte "
//                        + "Blanche, or any other brand that MasterCard reasonably determines to be in competition with ", font3) ;
//                c3.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c3 ) ;
//                
//                Paragraph c4 = new Paragraph("MasterCard, and 'Competing Card' means any payment "
//                        + "card bearing a Competing Brand or whose transactions are interchanged within a ", font3) ;
//                c4.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c4 ) ;
//                
//                Paragraph c5 = new Paragraph("Competing Brand. Strategic Partner will be responsible "
//                        + "for all aspects of carrying out any promotion, product launch, meeting or event for which ", font3) ;
//                c5.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c5 ) ;
//                
//                Paragraph c6 = new Paragraph("the Support may be used, including, without limitation, "
//                        + "the preparation, production, operation and realization of any such promotion, product ", font3) ;
//                c6.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c6 ) ;
//                
//                Paragraph c7 = new Paragraph("launch, meeting or event. ", font3) ;
//                c7.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c7 ) ;
//                
//                Paragraph c8 = new Paragraph("2. Indemnity: Strategic Partner shall indemnify and hold harmless "
//                        + "MasterCard from and against any and all actions, proceedings, losses, ", font3) ;
//                c8.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c8 ) ;
//                
//                Paragraph c9 = new Paragraph("costs, expenses (including, without limitation, the reasonable fees and "
//                        + "expenses of counsel for MasterCard), proceedings, claims and/or ", font3) ;
//                c9.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c9 ) ;
//                
//                Paragraph c10 = new Paragraph("demands in any way arising out of the acts or omissions and/or the "
//                        + "performance or failure to perform by Strategic Partner or any third ", font3) ;
//                c10.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c10 ) ;
//                
//                Paragraph c11 = new Paragraph("party providers to Strategic Partner in connection with the matters set "
//                        + "forth in this Agreement, including the preparation, production, ", font3) ;
//                c11.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c11 ) ;
//                
//                Paragraph c12 = new Paragraph("operation and realization of any promotion, product launch, meeting or "
//                        + "event.", font3) ;
//                c12.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c12 ) ;
//                
//                Paragraph c13 = new Paragraph("3. Promotions: In the event this Agreement involves Strategic Partner "
//                        + "carrying out a promotion, contest or similar event, Strategic Partner ", font3) ;
//                c13.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c13 ) ;
//                
//                Paragraph c14 = new Paragraph("shall be responsible for conducting the promotion or contest or "
//                        + "otherwise, in accordance with all applicable laws and regulations, ", font3) ;
//                c14.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c14 ) ;
//                
//                Paragraph c15 = new Paragraph("including without limitations, any requirements of prior filings with or "
//                        + "authorizations from any regulatory authority and the payment of any ", font3) ;
//                c15.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c15 ) ;
//                
//                Paragraph c16 = new Paragraph("taxes that may apply to the distribution of prizes or otherwise. ", font3) ;
//                c16.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c16 ) ;
//                
//                Paragraph c17 = new Paragraph("4. Approvals: Strategic Partner and MasterCard shall mutually agree as to "
//                        + "MasterCard's brand presence in any materials for which Support is ", font3) ;
//                c17.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c17 ) ;
//                
//                Paragraph c18 = new Paragraph("provided hereunder. Strategic Partner shall submit all materials for prior "
//                        + "written approval of MasterCard. All materials must comply with ", font3) ;
//                c18.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c18 ) ;
//                
//                Paragraph c19 = new Paragraph("MasterCard's brand standards as revised from time to time. ", font3) ;
//                c19.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c19 ) ;
//                
//                Paragraph c20 = new Paragraph("5. Successors: Continued Observance. The obligations stated herein shall "
//                        + "be binding upon and inure to the benefit of each of the parties and their ", font3) ;
//                c20.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c20 ) ;
//                
//                Paragraph c21 = new Paragraph("respective successors and assigns, provided however, that Strategic "
//                        + "Partner shall not have the right to assign to any third party by contract, ", font3) ;
//                c21.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c21 ) ;
//                
//                Paragraph c22 = new Paragraph("by operation of law or otherwise, any of its rights against MasterCard, "
//                        + "or the benefits thereof, without first obtaining the prior written consent ", font3) ;
//                c22.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c22 ) ;
//                
//                Paragraph c23 = new Paragraph("of MasterCard. If any Person acquires any interest in this Agreement or "
//                        + "the subject matter hereof in any manner, whether by contract, ", font3) ;
//                c23.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c23 ) ;
//                
//                Paragraph c24 = new Paragraph("operation of law or otherwise, such interest shall be held subject to all "
//                        + "of the terms of this Agreement and by taking or holding such interest, ", font3) ;
//                c24.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c24 ) ;
//                
//                Paragraph c25 = new Paragraph("such Person shall be conclusively deemed to have agreed to be bound "
//                        + "by, and to comply with, all of these Terms and Conditions. ", font3) ;
//                c25.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c25 ) ;
//                
//                Paragraph c26 = new Paragraph("6. Confidentiality: The terms of this Agreement and all strategies, "
//                        + "financial, customer and other proprietary information of both parties ", font3) ;
//                c26.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c26 ) ;
//                
//                Paragraph c27 = new Paragraph("will remain confidential and will not be disclosed to any third party "
//                        + "during the period of the Initiative and for a period of three (3) years ", font3) ;
//                c27.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c27 ) ;
//                
//                Paragraph c28 = new Paragraph("thereafter; provided, that the parties may disclose such information to "
//                        + "their accountants, attorneys, financial officers and employees to the ", font3) ;
//                c28.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c28 ) ;
//                
//                Paragraph c29 = new Paragraph("extent necessary to perform their duties (provided that all such persons "
//                        + "agree to comply with the confidentiality provisions hereof). ", font3) ;
//                c29.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c29 ) ;
//                
//                Paragraph c30 = new Paragraph("7. Miscellaneous: This Agreement and the transactions contemplated "
//                        + "hereby may be terminated at any time by either party by giving the ", font3) ;
//                c30.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c30 ) ;
//                
//                Paragraph c31 = new Paragraph("other thirty (30) days advance written notice. Upon such termination, "
//                        + "neither party shall have any continuing obligation hereunder except ", font3) ;
//                c31.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c31 ) ;
//                
//                Paragraph c32 = new Paragraph("that Strategic Partner promptly will return to MasterCard any unspent "
//                        + "funds paid or Support provided by MasterCard hereunder prior to the ", font3) ;
//                c32.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c32 ) ;
//                
//                Paragraph c33 = new Paragraph("date of termination. Any provisions that, by their terms, should survive "
//                        + "termination of this Agreement shall survive the termination hereof. This ", font3) ;
//                c33.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c33 ) ;
//                
//                Paragraph c34 = new Paragraph("Agreement evidences the entire agreement and understanding "
//                        + "between MasterCard and Strategic Partner with respect to the Initiative ", font3) ;
//                c34.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c34 ) ;
//                
//                Paragraph c35 = new Paragraph("described herein and supersedes all prior agreements between the "
//                        + "parties in respect thereof. This Agreement and the respective rights and ", font3) ;
//                c35.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c35 ) ;
//                
//                Paragraph c36 = new Paragraph("obligations of the parties hereto shall be governed by the laws of the "
//                        + "State of New York without reference to its conflict-of-laws or similar ", font3) ;
//                c36.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c36 ) ;
//                
//                Paragraph c37 = new Paragraph("provisions that would mandate or permit application of the substantive "
//                        + "law of any other jurisdiction. Both parties hereby expressly waive any ", font3) ;
//                c37.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c37 ) ;
//                
//                Paragraph c38 = new Paragraph("other jurisdiction that may correspond to them by reason of its present "
//                        + "and/or future domicile or otherwise. This Confirmation may be ", font3) ;
//                c38.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c38 ) ;
//                
//                Paragraph c39 = new Paragraph("executed in one or more counterparts, each of which, taken together, "
//                        + "shall constitute but one original document. ", font3) ;
//                c39.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c39 ) ;
//                
//                Paragraph c40 = new Paragraph("8. Business integrity: ", font3) ;
//                c40.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c40 ) ;
//                
//                Paragraph c41 = new Paragraph("a) No bribery: Strategic Partner represents, warrants and agrees to "
//                        + "abide by all applicable anti-corruption laws (including, but not limited ", font3) ;
//                c41.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c41 ) ;
//                
//                Paragraph c42 = new Paragraph("to, the U. S. Foreign Corrupt Practices Act, the UK Bribery Act and all "
//                        + "comparable local anti-bribery laws and regulations), and neither will ", font3) ;
//                c42.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c42 ) ;
//                
//                Paragraph c43 = new Paragraph("engage in making nor accepting any corrupt payments, kickbacks or "
//                        + "other improper payments in connection with or in furtherance of the ", font3) ;
//                c43.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c43 ) ;
//                
//                Paragraph c44 = new Paragraph("Agreement. Likewise, Strategic Partner shall comply, and shall ensure "
//                        + "that each of its subcontractors and personnel complies, with all such ", font3) ;
//                c44.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c44 ) ;
//                
//                Paragraph c45 = new Paragraph("applicable anti-bribery laws. Failure by Strategic Partner to comply with "
//                        + "the terms of this clause will constitute a material breach of the ", font3) ;
//                c45.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c45 ) ;
//                
//                Paragraph c46 = new Paragraph("Agreement. ", font3) ;
//                c46.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c46 ) ;
//                
//                Paragraph c47 = new Paragraph("b) Code of Conduct/Conflicts of Interest: Strategic Partner shall conduct "
//                        + "business substantially in consistency with the standards of MasterCard’s ", font3) ;
//                c47.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c47 ) ;
//                
//                Paragraph c48 = new Paragraph("Code of Conduct. Failure to do so may result in MasterCard terminating "
//                        + "the Agreement with Strategic Partner. The MasterCard Code of ", font3) ;
//                c48.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c48 ) ;
//                
//                Paragraph c49 = new Paragraph("Conduct is available for reference on the MasterCard website under "
//                        + "Corporate Governance (within “About MasterCard”). ", font3) ;
//                c49.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c49 ) ;
//                
//                Paragraph c50 = new Paragraph("c) Strategic Partner acknowledges and agrees that MasterCard has no "
//                        + "role to play in the selection of the promotion winner, particularly in the ", font3) ;
//                c50.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c50 ) ;
//                
//                Paragraph c51 = new Paragraph("event that MasterCard provides the Support to Strategic Partner for an "
//                        + "Initiative that consists of a promotion or campaign with Strategic ", font3) ;
//                c51.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c51 ) ;
//                
//                Paragraph c52 = new Paragraph("Partner’s staff (i.e., internal sales promotion, acquisition or activation "
//                        + "card campaign, etc.). ", font3) ;
//                c52.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c52 ) ;
//                
//                Paragraph c53 = new Paragraph("9. Initiative Targets: Strategic Partner shall exercise its best efforts to "
//                        + "achieve the card issuance and/or volume targets associated with the ", font3) ;
//                c53.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c53 ) ;
//                
//                Paragraph c54 = new Paragraph("Initiative and set forth in this Agreement (the “Target(s)”). In the event "
//                        + "that notwithstanding its best efforts, Strategic Partner achieves less ", font3) ;
//                c54.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c54 ) ;
//                
//                Paragraph c55 = new Paragraph("than 80% of such Target(s), MasterCard may, at its sole discretion, "
//                        + "withhold delivery of the Support to Strategic Partner. ", font3) ;
//                c55.setAlignment( Paragraph.ALIGN_JUSTIFIED ) ; documento.add( c55 ) ;
                
                documento.setMargins(25,25,25,25);
			
		documento.close();
                
                response.setHeader("Expires", "0");
                response.setHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
                response.setHeader("Pragma", "public");
                response.setHeader("Content-Disposition", "attachment;filename=Contrato.pdf") ;
                // Configuramos el content type
                response.setContentType("application/pdf");
                // Tamaño
                response.setContentLength(baos.size());
                // Esccribir el ByteArrayOutputStream a el ServletOutputStream
                OutputStream os = response.getOutputStream();
                baos.writeTo(os);
                os.flush();
                os.close();
                    
                }catch( Exception e )
                {
                  System.out.println("no se creo el pdf");  
                }
//            return mapping.findForward( "success" ) ;
                return null ;
        }
    
}
